package com.gopay.scheduler.job;

import com.gopay.common.constants.dic.GopayGlobalVariablesConstant;
import com.gopay.common.dss.tools.FuseTool;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.dao.AcpsResourceIsoLogQueryDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 长时间资源未解冻定时任务
 * Created by Light on 2017/7/28.
 */
public class IsolationCheckJob extends GopayBaseJob{

    private static final Logger logger = LoggerFactory.getLogger(IsolationCheckJob.class);

    @Resource(name = "fuseTool")
    private FuseTool fuseTool;

    @Resource(name = "acpsResourceIsoLogQueryDAO")
    private AcpsResourceIsoLogQueryDAO acpsResourceIsoLogQueryDAO;

    private static final int maxFuseTimes = 1;

    @Value("#{webProperties['IsolationCheckJob.shortTimeIntervalTime']}")
    private long shortTimeIntervalTime;

    @Value("#{webProperties['IsolationCheckJob.shortTimeUnfreezeMaxSize']}")
    private int shortTimeUnfreezeMaxSize;

    @Value("#{webProperties['IsolationCheckJob.longTimeIntervalTime']}")
    private long longTimeIntervalTime;

    @Value("#{webProperties['IsolationCheckJob.longTimeUnfreezeMaxSize']}")
    private int longTimeUnfreezeMaxSize;

    @Override
    protected void doJobInternal() {
        try{
            logger.info("定时检测未冻结数据开始");
            Date date = new Date(System.currentTimeMillis() - shortTimeIntervalTime);
            int byCreateTs = acpsResourceIsoLogQueryDAO.findByCreateTs(date);
            logger.info("{}毫秒内数据有{}条", shortTimeIntervalTime, byCreateTs);
            if(shortTimeUnfreezeMaxSize <= byCreateTs){
                fuseTool.fuse(GopayGlobalVariablesConstant.ISOLATION_CHECK_WARN_SWITCH, GopayGlobalVariablesConstant.ISOLATION_CHECK_WARN_MOBILE, maxFuseTimes);
            }
            date = new Date(System.currentTimeMillis() - longTimeIntervalTime);
            byCreateTs = acpsResourceIsoLogQueryDAO.findByCreateTs(date);
            logger.info("{}毫秒内数据有{}条", longTimeIntervalTime, byCreateTs);
            if(longTimeUnfreezeMaxSize <= byCreateTs){
                fuseTool.fuse(GopayGlobalVariablesConstant.ISOLATION_CHECK_WARN_SWITCH, GopayGlobalVariablesConstant.ISOLATION_CHECK_WARN_MOBILE, maxFuseTimes);
            }
            logger.info("定时检测未冻结数据结束");
        } catch (Exception e){
            logger.error("检查长时间未解冻预警异常",e);
        }
    }

    public long getShortTimeIntervalTime() {
        return shortTimeIntervalTime;
    }

    public void setShortTimeIntervalTime(long shortTimeIntervalTime) {
        this.shortTimeIntervalTime = shortTimeIntervalTime;
    }

    public int getShortTimeUnfreezeMaxSize() {
        return shortTimeUnfreezeMaxSize;
    }

    public void setShortTimeUnfreezeMaxSize(int shortTimeUnfreezeMaxSize) {
        this.shortTimeUnfreezeMaxSize = shortTimeUnfreezeMaxSize;
    }

    public long getLongTimeIntervalTime() {
        return longTimeIntervalTime;
    }

    public void setLongTimeIntervalTime(long longTimeIntervalTime) {
        this.longTimeIntervalTime = longTimeIntervalTime;
    }

    public int getLongTimeUnfreezeMaxSize() {
        return longTimeUnfreezeMaxSize;
    }

    public void setLongTimeUnfreezeMaxSize(int longTimeUnfreezeMaxSize) {
        this.longTimeUnfreezeMaxSize = longTimeUnfreezeMaxSize;
    }
}
